import { getYMD, getNowFormatDate, getDay, formatGMTDate, sortBy, toPercent } from '../../../assets/js/util.js'

export default {
    data() {
        return {
            // 默认参数 和 URL
            SRC: '',
            DP: {
                p: '',
                f: '',
                uid: ''
            },
            region: [],
            projects: [],
            // form输入框的胡宽度
            formLabelWidth: '150px',
            /*initWidth 的宽*/
            initWidth: '100%',
            // 添加或搜索数据
            ruleForm: {
                id: "",
                name: '',
                serverType: '',
                capacity: '',
                description: '',
                used: true,
                name: '',
                channelIds: []
            },
            /*当前页显示数量*/
            pageSize: 10,
            /*每页显示的数量*/
            pageSizes: [10, 20, 50, 100],
            /*dataTotal表数量*/
            dataTotal: 0,
            /*分页器*/
            currentPage: 1,
            // 游戏列表数据
            gameList: [
                // {
                //   value: '权力与荣耀',
                //   label: '权力与荣耀'
                // }, {
                //   value: '九州天空城',
                //   label: '九州天空城'
                // }, {
                //   value: '琅琊榜',
                //   label: '琅琊榜'
                // }
            ],
            // 区服渠道
            channelNameList: [],
            channelNameList1: [],
            channelNameList2: [],
            // qufufenzu
            serverTypeList: [
                { "label": "混服", "value": "1" },
                { "label": "ios正版", "value": "2" },
                { "label": "应用宝", "value": "3" },
                { "label": "硬核联盟", "value": "4" }
            ],
            // 验证ruleForm
            rules: {},
            tableData: [],
            tableDataAll: [],

            // 全选
            chIds1: '',
            checkAllChIds1: false,
            checkedChIds1: [],
            chIds2: '',
            checkAllChIds2: false,
            checkedChIds2: [],


        }
    },
    methods: {
        successFun(data) {
            this.$message({
                message: data,
                type: 'success'
            });
        },
        errorFun(data) {
            this.$message.error(data);
        },
        /*分页*/
        handleSizeChange(val) {
            this.pageSize = val;
            this.commonAjax();
        },
        handleCurrentChange(val) {
            this.currentPage = val;
            this.commonAjax();
        },
        // 获取数据 获取工程List 以及页面展示数据
        getProjectData() {
            this.projects = [];
            this.$http.post(`${this.SRC}project/list.do`).then(
                function(res) {
                    var data = res.body;
                    if (data["code"] === 0) {
                        for (var i = 0; i < data['msg'].length; i++) {
                            var obj = {
                                label: data["msg"][i]['projectName'],
                                value: data["msg"][i]['id']
                            }
                            this.projects.push(obj);
                        }
                        this.region = this.projects[1].value;
                        this.commonAjax();
                    } else {
                        this.errorFun(data['msg'])
                    }
                }
            )
        },
        // 选择游戏
        projectsChange(val) {
            var p = this.projects;
            for (var i = 0; i < p.length; i++) {
                if (val === p[i].value) {
                    this.ruleForm.gameName = p[i].label;
                    this.ruleForm.gameId = p[i].value;
                }
            }
        },
        seFn(o) {
            this.ruleForm.serverType = o;
        },
        handleCheckAllChangeChIds2($event, scope) {
            this.checkedChIds2 = [];
            for (var i of this.channelNameList2) {
                this.checkedChIds2.push(i.value)
            }
            this.checkedChIds2 = event.target.checked ? this.checkedChIds2 : [];
            scope.row.channelIds = this.checkedChIds2;
        },
        handleCheckedChangeChIds2(value) {
            let checkedCount = value.length;
            this.checkAllChIds2 = checkedCount === this.channelNameList2.length;
        },
        chIdsVa2(o) {
            if (o == "") {
                if (this.checkedChIds2.length != this.channelNameList2.length) {
                    this.checkAllChIds2 = false;
                }
            }
            this.channelNameList2 = this.channelNameList.filter(function(v) {
                return v.label.toLowerCase().indexOf(o.toLowerCase()) !== -1;
            })
        },
        handleCheckAllChangeChIds1(event) {
            this.checkedChIds1 = [];
            for (var i of this.channelNameList1) {
                this.checkedChIds1.push(i.value)
            }
            this.checkedChIds1 = event.target.checked ? this.checkedChIds1 : [];
        },
        handleCheckedChangeChIds1(value) {
            let checkedCount = value.length;
            this.checkAllChIds1 = checkedCount === this.channelNameList1.length;
        },
        chIdsVa1(o) {
            if (o == "") {
                if (this.checkedChIds1.length != this.channelNameList.length) {
                    this.checkAllChIds1 = false;
                }
            }
            this.channelNameList1 = this.channelNameList.filter(function(v) {
                return v.label.toLowerCase().indexOf(o.toLowerCase()) !== -1;
            })

        },
        handleCurrentChange(o) {
            this.currentPage = o;
            this.commonAjax()
        },
        chaNameFn() {
            this.$http.post(`${this.SRC}serverconfig/channellist.do?gameId=${this.DP.p}`).then(function(data) {
                var data = data.body;
                if (data.code == 0) {
                    var td = data.msg;
                    for (var i of td) {
                        var obj = {};
                        obj.value = i[0];
                        obj.label = i[1];
                        this.channelNameList.push(obj)
                    }
                    this.channelNameList1 = this.channelNameList.slice();
                    this.channelNameList2 = this.channelNameList.slice();
                } else {
                    this.errorFun(data['msg'])
                }
            }).catch(function(res) {})
        },
        commonAjax() {
            /*this.region = this.region;*/
            this.$http.post(`${this.SRC}serverconfig/getlist.do`, {
                gameId: this.region,
                curPage: this.currentPage,
                pageSize: this.pageSize,
                id: this.ruleForm.id,
                name: this.ruleForm.name,
                description: this.ruleForm.description,
                capacity: this.ruleForm.capacity,
                channelIds: this.checkedChIds1.join(","),
                serverType: this.ruleForm.serverType,
            }, {
                emulateJSON: true
            }).then(function(data) {
                var dat = data.body;
                if (dat.code == 0) {
                    var td = dat.msg.msg.data;
                    for (var i of td) {
                        for (var j in i.channelNameList) {
                            var obj = {};
                            obj.label = i.channelNameList[j];
                            obj.value = i.channelIdList[j];
                            i.channelNameList[j] = obj
                        }
                        i.id = parseInt(i.id);
                        i.channelIds = i.channelIds.split(',');
                        for (var n = 0; n < i.channelIds.length; n++) {
                            if (i.channelIds[n] == "" || i.channelIds[n] == "undefined") {
                                i.channelIds.splice(n, 1);
                                n = n - 1
                            }
                        }
                    }
                    this.tableData = sortBy(td, 'id');
                    this.dataTotal = dat.msg.msg.totalSize;

                } else {
                    this.errorFun(data['msg'])
                }
            }).catch(function(res) {})
        },
        /*设置初始值*/
        _init() {
            /*this.commonAjax()*/
            window.addEventListener('resize', function() {
                /*修改表格的宽度*/
                this.initWidth = document.body.clientWidth - 220 + "px";
            }.bind(this))
        },
        // 修改用户
        userEdit(index, row) {
            this.revampForm = row;
            this.revampForm.password = '';
            this.userShow = true;
        },
        // 编辑权限
        edit(index, row) {
            this.$http.post(`${this.SRC}serverconfig/modify.do`, {
                gameId: row.gameId,
                id: row.id,
                name: row.name,
                description: row.description,
                serverType: row.serverType,
                capacity: row.capacity,
                channelIds: row.channelIds.join(',')
            }, {
                emulateJSON: true
            }).then(function(data) {
                var data = data.body;
                if (data.code == 0) {
                    this.successFun(data['msg']);
                } else {
                    this.errorFun(data['msg'])
                }
            }).catch(function(res) {});
        },
        // 删除用户
        handleDelete(index, row) {
            this.$http.post(`${this.SRC}serverconfig/delete.do`, {
                id: row.id
            }, {
                emulateJSON: true
            }).then(function(data) {
                var data = data.body;
                if (data.code == 0) {
                    this.tableData = this.tableData.filter(function(v) {
                        return v.id != row.id
                    })
                    this.successFun(data['msg']);
                    this.dataTotal = this.dataTotal - 1
                } else {
                    this.errorFun(data['msg'])
                }
            }).catch(function(res) {});
        },
        submitForm(formName) {
            var that = this;
            var p = new Promise(function(resolve, reject) {
                that.$http.post(`${that.SRC}serverconfig/add.do`, {
                    gameId: that.DP.p,
                    id: that.ruleForm.id,
                    name: that.ruleForm.name,
                    description: that.ruleForm.description,
                    capacity: that.ruleForm.capacity,
                    channelIds: that.checkedChIds1.join(","),
                    serverType: that.ruleForm.serverType,

                }, {
                    emulateJSON: true
                }).then(function(data) {
                    return resolve(data.body)
                })
            });
            p.then(function(dataP) {
                that.$http.post(`${that.SRC}serverconfig/list.do`, {
                    curPage: that.currentPage,
                    pageSize: that.pageSize
                }, {
                    emulateJSON: true
                }).then(function(data) {
                    var data = data.body;
                    if (dataP.code == 0 && data.code == 0) {
                        var td = data.msg.data
                        for (var i of td) {
                            for (var j in i.channelNameList) {
                                var obj = {};
                                obj.label = i.channelNameList[j];
                                obj.value = i.channelIdList[j];
                                i.channelNameList[j] = obj
                            }
                            i.id = parseInt(i.id);
                            i.channelIds = i.channelIds.split(',');
                            for (var n = 0; n < i.channelIds.length; n++) {
                                if (i.channelIds[n] == "" || i.channelIds[n] == "undefined") {
                                    i.channelIds.splice(n, 1);
                                    n = n - 1
                                }
                            }
                        }

                        that.tableData = sortBy(td, 'id');
                        that.dataTotal = data.msg.totalSize;


                        that.successFun(dataP['msg']);
                        that.$refs[formName].resetFields();
                        that.checkedChIds1 = '';
                    } else {
                        that.errorFun(dataP['msg'])
                    }
                }).catch(function(res) {});
            })
        },
        searchForm() {
            this.commonAjax()
                // http://10.236.44.188:8080/serverconfig/list.do?curPage=1&pageSize=10
                // this.$refs[formName].resetFields();
        }
    },
    mounted() {
        this.SRC = this.$store.getters.src3;
        this.DP.p = localStorage.getItem('ZULONGBIPROJECTID');
        this._init();
        this.getProjectData();

        /*this.chaNameFn();*/
    }
}